import Cookies from 'js-cookie';
import * as types from '../mutation-types';

const APP_COOKIE = 'USER_CASE_INFO'; // 登录保存的标识
const DICT_DF_ALL = 'DICT_DF_ALL'; // 查询字典表所有信息


const state = {
    // 系统管理
    isSystem: false,
    userInfo: {},
    // 菜单数据
    userMenus: [],
    // 没有处理数
    unProcessCounts: null,
    // 面包屑数据
    breadData: []
}

const mutations = {
    [types.SET_USER_INFO](state, data) {
        state.userInfo = data
        Cookies.set(APP_COOKIE, JSON.stringify(data))
    },
    [types.LOGIN_OUT](state, callback) {
        api.logout().then((data) => {
            callback(data)
        }).catch(res => {
            console.log(res)
        })
    },
    [types.CLEAR_USER_INFO](state, callback) {
        Cookies.remove(APP_COOKIE)
        state.userInfo = {}
        state.userMenus = []
        if (typeof callback === 'function') {
            callback()
        }
    },
    [types.GET_USER_MENUS](state, callback) {
        api.userMenus().then(data => {
            state.userMenus = data
            if (typeof callback === 'function') {
                callback(data)
            }
        }).catch(res => {
            console.log(res)
        })
    },
    [types.GET_UNPROCESS](state) {
        api.countByUserId().then((data) => {
            state.unProcessCounts = data
        }).catch(res => {
            console.log(res)
        })
    },
    [types.CLEAR_USER_MENUS](state) {
        state.userMenus = []
    },
    [types.SET_BREAD_DATA](state, data) {
        state.breadData = data
    },
    [types.QUERY_DICT](state, param) {
        var _type = param.type
        api.queryDict(param).then(data => {
            localStorage.setItem(_type, JSON.stringify(data.list))
        }).catch(res => {
            console.log(res)
        })
    },
    [types.QUERY_DICT_ALL](state) {
        api.queryAllDict().then(data => {
            localStorage.setItem(DICT_DF_ALL, JSON.stringify(data))
        }).catch(res => {
            console.log(res)
        })
    }
}

const getters = {
    // 获取用户信息
    userInfo: state => {
        let _userInfo = Cookies.get(APP_COOKIE)
        state.userInfo = _userInfo ? JSON.parse(_userInfo) : {}
        return state.userInfo
    },
    // 获取菜单
    userMenus: state => state.userMenus,
    // 获取没有处理数
    getUnProcess: state => state.unProcessCounts,
    // 获取面包屑数据
    breadData: state => state.breadData,
    // 风险类別
    riskCategory: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).risk_category : []
    },
    // 风险类型
    riskType: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).risk_type : []
    },
    // 风险等级
    riskLevel: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).risk_level : []
    },
    // 风险处理时长
    riskDisposeDate: state => {
        return [
            {
                value: 1,
                name: '三个工作日'
            }, {
                value: 2,
                name: '七个工作日'
            }
        ]
    },
    // 风险详情描述
    riskDetails: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).risk_details : []
    },
    // 产品类型类别 
    productType: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).product_type : []
    },
    // 案件/线索来源渠道
    sourceType: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).source_type : []
    },
    // 机构简称
    orgType: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).org_type : []
    },
    // 案件类型
    caseType: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).case_type : []
    },
    // 案件状态
    caseStatus: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).case_status : []
    },
    // 线索状态
    clueStatus: state => {
        let _types = localStorage.getItem(DICT_DF_ALL)
        return _types ? JSON.parse(_types).clue_status : []
    }
}

const actions = {
    clearUserInfo: ({ commit }, params) => commit(types.CLEAR_USER_INFO, params),
    loginOut: ({ commit }, params) => {
        commit(types.LOGIN_OUT, () => {
            commit(types.CLEAR_USER_INFO)
            if (typeof params === 'function') {
                params()
            }
        })
    },
    userMenus: ({ commit }, params) => commit(types.GET_USER_MENUS, params),
    getUnProcess: ({ commit }) => commit(types.GET_UNPROCESS),
    clearUserMenus: ({ commit }) => commit(types.CLEAR_USER_MENUS),
    setBreadData: ({ commit }, params) => commit(types.SET_BREAD_DATA, params),
    queryDict: ({ commit }, params) => commit(types.QUERY_DICT, params),
    queryAllDict: ({ commit }) => commit(types.QUERY_DICT_ALL)
}

export default {
    state,
    getters,
    mutations,
    actions
}